package cisdi.mes.sm.rm.serviceImpl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cisdi.mes.sm.rm.dao.RmBaseDao;
import cisdi.mes.sm.rm.service.BaseServ;
import cisdi.mes.sm.util.dao.BaseDao;

@Service("baseServImpl")
public class BaseServImpl implements BaseServ{
	
	@Resource(name="rmBaseDaoImpl")
	private RmBaseDao rmBaseDao;
	
	@Resource
	private BaseDao baseDao;

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public void save(Object entity) throws Exception {
		rmBaseDao.update(entity);
	}

	@Override
	public void add(Object entity) throws Exception {
		rmBaseDao.save(entity);
	}

	@Override
	public void delete(Object entity) throws Exception {
		rmBaseDao.delete(entity);
	}

	@Override
	public void delete(Object[] entities) throws Exception {
		ArrayList<Object> entitys = new ArrayList<Object>(entities.length);
		for(int i =0;i<entities.length;i++){
			entitys.add(entities[i]);
		}
		try {
			rmBaseDao.delete(entitys);
			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("删除失败！");
		}
	}
	
	@Override
	public void delete(String className, String... ids) throws Exception {
		Class<?> clazz = Class.forName(className);
		Field[] fields = clazz.getDeclaredFields();
		String primaryKey=null;
		for(Field field : fields){
			Annotation[] as = field.getDeclaredAnnotations();
			for(Annotation a : as){
				if(a.annotationType().getName().equalsIgnoreCase("javax.persistence.Id")){
					primaryKey = field.getName();
					break;
				}
			}
		}
		if(primaryKey!=null){
			try {
				rmBaseDao.delete(className, primaryKey, ids);
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("删除失败！");
			}
		} else {
			throw new Exception("找不到主键");
		}
	}

	@Override
	public Map<String, Object> query(String queryString) {
		Map<String, Object> gridData = new HashMap<String, Object>();
		List<?> list = baseDao.findAll(queryString);
		gridData.put("rows", list);
		gridData.put("total", list.size());
		return gridData;
	}

	@Override
	public Map<String, Object> query(String queryString, Integer offset,
			Integer length) {
		Map<String, Object> gridData = new HashMap<String, Object>();
		List<?> list = baseDao.findAll(queryString, offset, length);
		int count = baseDao.getTotalCount(queryString);
		gridData.put("rows", list);
		gridData.put("total", count);
		return gridData;
	}

	@Override
	public Map<String, Object> query(String queryString, String[] paramNames,
			Object[] values) {
		Map<String, Object> gridData = new HashMap<String, Object>();
		List<?> list = baseDao.findEntitiesByFilter(queryString, paramNames, values);
		gridData.put("rows", list);
		gridData.put("total", list.size());
		return gridData;
	}

	@Override
	public Map<String, Object> query(String queryString, String[] paramNames,
			Object[] values, Integer offset, Integer length) {
		Map<String, Object> gridData = new HashMap<String, Object>();
		List<?> list = baseDao.findEntitiesByFilter(queryString, paramNames, values, offset, length);
		int count = baseDao.getTotalCount(queryString, paramNames, values);
		gridData.put("rows", list);
		gridData.put("total", count);
		return gridData;
	}

}
